package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.edu.internship.busi.dto.DictionaryOptionDTO;
import com.edu.internship.busi.entity.DictionaryOption;
import com.edu.internship.busi.service.IDictionaryOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 *
 * @author lishiquan
 * @description <p>DictionaryOption 的controller</p>
 * @date 2020-07-06
 */
@RestController
@RequestMapping("api/dictionary-option")
@Slf4j
@Api(tags = {"字典项的控制器"})
public class DictionaryOptionController {

    @Resource
    private IDictionaryOptionService dictionaryOptionService;

    @GetMapping
    @ApiOperation(value = "查询-DictionaryOption", notes = "根据条件进行查询-DictionaryOption")
    public List<DictionaryOption> list(DictionaryOptionDTO dto) {
        LambdaQueryWrapper<DictionaryOption> dictionaryOptionQw = Wrappers.lambdaQuery();
        dictionaryOptionQw.eq(DictionaryOption::getDictId, dto.getDictId())
                .like(StringUtils.isNotBlank(dto.getDictLabel()), DictionaryOption::getDictLabel, dto.getDictLabel())
                .orderByAsc(DictionaryOption::getOrd);
        return dictionaryOptionService.list(dictionaryOptionQw);
    }

    @PostMapping("create")
    @ApiOperation(value = "新增-DictionaryOption", notes = "新增-DictionaryOption")
    public void create(@Validated @RequestBody DictionaryOptionDTO dto) {
        dictionaryOptionService.createDictionaryOption(dto);
    }

    @PostMapping("update")
    @ApiOperation(value = "更新-DictionaryOption", notes = "更新-DictionaryOption")
    public void update(@Validated @RequestBody DictionaryOptionDTO dto) {
        dictionaryOptionService.updateDictionaryOption(dto);
    }

    @PostMapping("remove")
    @ApiOperation(value = "删除-DictionaryOption", notes = "删除-DictionaryOption")
    public void remove(@RequestBody List<String> ids) {
        dictionaryOptionService.removeDictionaryOption(ids);
    }

}